<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-10-9
 * 版    本：1.0.0
 * 功能说明：商品品牌控制器。
 *
 **/

namespace Qwadmin\Controller;


class GoodsBrandController extends ComController {

    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('goods_brand');
        $pagesize = 10;#每页数量
        $count = $m->count();
        $list = $m->select();
        $page = new \Think\Page($count, $pagesize);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    /*
  * 添加商品品牌
  * */
    public function add()
    {
        $this->display('form');
    }

    /*
    * 删除商品品牌
    * */
    public function del()
    {

        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        //uid为1的禁止删除
        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                //当前品牌下有商品的禁止删除
                if (M('goods')->where(array('goods_brand_id' => intval($ids)))->select()) {
                    $this->error('当前品牌下有商品不能删除！');
                }
                $ids[$k] = intval($v);
            }
        }elseif($ids){
            //当前品牌下有商品的禁止删除
            if (M('goods')->where(array('goods_brand_id' => intval($ids)))->select()) {
                $this->error('当前品牌下有商品不能删除！');
            }
        }else{
            $this->error('参数错误！');
            }

        $map['id'] = array('in', $ids);
        if (M('goods_brand')->where($map)->delete()) {
            addlog('删除品牌ID：' . $ids);
            $this->success('恭喜，品牌删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

    public function edit($id = 0)
    {
        $id = intval($id);
        $m = M('goods_brand');
        $currentmenu = $m->where("id='$id'")->find();
        if (!$currentmenu) {
            $this->error('参数错误！');
        }

        $option = $m->select();
        $this->assign('option', $option);
        $this->assign('currentmenu', $currentmenu);
        $this->display('form');
    }

    public function update()
    {
        $m = M('goods_type');
        $id = I('post.id', '', 'intval');
        $data['pid'] = I('post.pid', '', 'intval');
        $data['name'] = I('post.name', '', 'strip_tags');
        $data['demo'] = I('post.demo');

        if ($id) {
            if(M('goods_brand')->where(array('name'=> $data['name'],"id"=>array('neq',$id)))->find()){
                $this->error('该品牌已经存在！');
            }
            M('goods_brand')->data($data)->where("id='{$id}'")->save();
            addlog('编辑品牌，ID：' . $id);
        } else {
                if(M('goods_brand')->where(array('name'=> $data['name']))->find()){
                    $this->error('该品牌已经存在！');
                }
                M('goods_brand')->data($data)->add();
                addlog('新增品牌，名称：' . $data['name']);
        }

        $this->success('操作成功！','index');
    }
} 